import pymysql
from connect import conf
import pandas as pd
import numpy as np


def init():
    # 连接数据库
    conn = pymysql.connect(
        host=conf.get_host(),
        port=conf.get_port(),
        user=conf.get_user(),
        password=conf.get_password(),
        database=conf.get_database(),
        charset='utf8'
    )

    return conn


def get_all_from_table(table_name):
    """
    获取表格table_name的所有数据
    :param table_name: string, 表名称
    :return: 该表的所有数据条目
    """

    # 查询表格所有数据
    sql = "select * from %s" % table_name
    df = pd.read_sql(sql, conn)
    return df


def select_col_from_table(col_name, table_name):
    """
    查找table_name表中col_name列的不同值
    :param col_name: 列名
    :param table_name: 表名
    :return: list
    """

    sql = "select distinct %s from %s" % (col_name, table_name)
    df = pd.read_sql(sql, con=conn)
    temp = np.array(df).tolist()
    res = [j for i in temp for j in i]
    return res


conn = init()
print(get_all_from_table("city"))
# print(select_col_from_table("Name", "city"))
